import store from '@/store/index'

function checkPermission(el, binding) {
  const { value } = binding

    console.log('store',store);


  const roles = store.getters && store.getters.roles
//  v-permission= [ 'admin']    instanceof  => 找父亲是谁  判断是不是数组

    // console.log("权限指令是否生效",roles);
  if (value && value instanceof Array) {
    if (value.length > 0) {
        //  [ 'admin'] 
      const permissionRoles = value

      const hasPermission = roles.some(role => {
        return permissionRoles.includes(role)
      })

    //    无权限  节点会被删除
      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    }
  } else {
    throw new Error(`need roles! Like v-permission="['admin','editor']"`)
  }
}

export default {
  inserted(el, binding) {
    checkPermission(el, binding)
  },
  update(el, binding) {
    checkPermission(el, binding)
  }
}